﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class QuickMasterControlPanel : UserControl
    {
        public event EventHandler WantUpload;

        public QuickMasterControlPanel()
        {
            InitializeComponent();
        }

        private void btnControlPanel_Click(object sender, RoutedEventArgs e)
        {
            popup.IsOpen = !popup.IsOpen;
            if (!debugPanel.HasBoundDebugInformation)
            {
                debugPanel.HasBoundDebugInformation = true;
                debugPanel.BindDebugInformation(DebugInformation.Instance);
            }
        }

        public void AppendText(string s)
        {
            Dispatcher.BeginInvoke(() => txtNotification.Text += Environment.NewLine + s);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            btnSave.IsEnabled = false;
            DataAccess.Client.SaveWorldAsync(DataAccess.AssignedUniqueId);
            DataAccess.Client.SaveWorldCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_SaveWorldCompleted);
        }

        void Client_SaveWorldCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                AppendText("Error Saving World: " + e.Error.ToString());
            }
            else
            {
                AppendText("World Saved.");
            }

            DataAccess.Client.SaveWorldCompleted -= Client_SaveWorldCompleted;
            Dispatcher.BeginInvoke(() => btnSave.IsEnabled = true);
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            if (WantUpload != null)
                WantUpload(this, EventArgs.Empty);
        }

        private void DownloadData_RequestComplete(object sender, ObjectEventArgs<Type, ByteDataMessage> e)
        {
            AppendText("Downloaded all objs of type " + e.obj1.Name + ", bytes: " + e.obj2.ByteCount.ToString());
        }

    }
}